package org.me.Anime.Utli;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.me.Anime.Data.Image;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class FileUtli {
	
	public static List<Image> GetImageFromAssets(Context mContext, String filename)
	{
		try
		{
			List<Image> image = new ArrayList<Image>();
			Log.e("GetImageFromAssets", "filename = "+filename);
			for(int i = 0; i< getImage(mContext,filename).size(); i++)
		    {
		    	
		    	Log.e("hungcx", "file = "+getImage(mContext,filename).get(i));
		    	image.add(new Image(getImage(mContext,filename).get(i)));
		    }
			return image;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
		return null;
	}
	
	
	public static String GetOneImage(Context mContext, String filename)
	{
		try
		{
		    	return getImage(mContext,filename).get(0);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
		return null;
	}
	
	
	private static List<String> getImage(Context mContext,String filename)
    {
		AssetManager assetManager = mContext.getAssets();
		String[] files = null;
		try 
		{
			files = assetManager.list(filename);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}   
		List<String> it=Arrays.asList(files);
		return it; 
      }
	
	
	public static InputStream in = null;
	public static ByteArrayOutputStream out = null;
	
	public static byte[] readFileFromAssets(Context context, String filename)
	{
		in  = null; 					
		out = null; 	
		byte[] readBuffer = new byte[128]; 	
		try
		{
			int size = 0;
			in  = context.getAssets().open(filename);
			out = new ByteArrayOutputStream();
			while ((size = in.read(readBuffer)) > 0)
			{
				out.write(readBuffer, 0, size);
			}
			out.close();
			in.close();
		}
		catch (Exception e)
		{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (Exception ignore)
				{
				}
			}
			if (in != null)
			{
				try
				{
					in.close();
				}
				catch (Exception ignore)
				{
				}
			}
			return null;
		}
		return out.toByteArray();
	}
	
}
